//
//  TTSPublicTableViewCell.m
//  TTSVideo
//
//  Created by chenHR on 16/3/1.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "TTSPublicTableViewCell.h"
#import <UIImageView+WebCache.h>
#import "TTSHotRankModel.h"

@interface TTSPublicTableViewCell ()
/**
 *  背景图片
 */
@property (nonatomic, strong) UIImageView *bgImgView;
/**
 *  标题Label
 */
@property (nonatomic, strong) UILabel *titleL;
/**
 *  副标题Label
 */
@property (nonatomic, strong) UILabel *subTitleL;
/**
 *  序号Label
 */
@property (nonatomic, strong) UILabel *numberL;
/**
 *  半透明的View
 */
@property (nonatomic, strong) UIView *view;

@end

@implementation TTSPublicTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self TTSCreateSubViews];
    }
    return self;
}

- (void)TTSCreateSubViews{
    // 背景图
    _bgImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 270)];
    _bgImgView.backgroundColor = [UIColor yellowColor];
    _bgImgView.userInteractionEnabled = YES;
    _bgImgView.image = [UIImage imageNamed:@"test.jpeg"];
    
    // 图片上面的View
    self.view = [[UIView alloc] initWithFrame:_bgImgView.frame];
    _view.backgroundColor = [UIColor blackColor];
    _view.userInteractionEnabled = YES;
    _view.alpha = 0.4;
    
    //标题
    _titleL = [[UILabel alloc] initWithFrame:CGRectMake(50, 50, 300, 40)];
    _titleL.text = @"";
    _titleL.backgroundColor = [UIColor clearColor];
    _titleL.textAlignment = NSTextAlignmentCenter;
    _titleL.font = [UIFont boldSystemFontOfSize:18];
    _titleL.alpha = 1;
    [_titleL setTextColor:[UIColor whiteColor]];
    [_titleL setShadowColor:[UIColor whiteColor]];
    
    // 副标题
    _subTitleL = [[UILabel alloc] initWithFrame:CGRectMake(_titleL.frame.origin.x + 60, _titleL.frame.origin.y + 50, 150, 40)];
    _subTitleL.text = @"";
    _subTitleL.backgroundColor = [UIColor clearColor];
    _subTitleL.textAlignment = NSTextAlignmentCenter;
    _subTitleL.font = [UIFont boldSystemFontOfSize:14];
    _subTitleL.alpha = 1;
    [_subTitleL setTextColor:[UIColor whiteColor]];
    
    // 序号
    _numberL = [[UILabel alloc] initWithFrame:CGRectMake(_subTitleL.frame.origin.x + 10, _subTitleL.frame.origin.y + 160, 45, 40)];
    _numberL.center = CGPointMake(WIDTH / 2, _subTitleL.frame.origin.y + 90);
    _numberL.backgroundColor = [UIColor redColor];
    
    _numberL.textAlignment = 1;
    [_numberL setTextColor:[UIColor whiteColor]];
    _numberL.font = [UIFont italicSystemFontOfSize:16];
    _numberL.text = @"";
    _numberL.alpha = 1;
    // 给label添加背景图片
    [_numberL setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"labelTitle6"]]];
    
    // 轻拍手势
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    // 给view添加轻拍手势
    [_view addGestureRecognizer:tapGesture];
    
    // 长按手势
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];
    [_view addGestureRecognizer:longPress];

    [_bgImgView addSubview:_view];
    [_bgImgView addSubview:_titleL];
    [_bgImgView addSubview:_subTitleL];
    [_bgImgView addSubview:_numberL];
    [self addSubview:_bgImgView];
    
}

// 取消tableviewCell的点击高亮效果
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated{
    
}
// 取消tableviewCell的点击效果
- (void)setSelected:(BOOL)selected animated:(BOOL)animated{
    
}

// 轻拍手势(推出下一个界面)
- (void)tapAction:(UITapGestureRecognizer *)tap{
    NSLog(@"轻拍");
    
    
    
}

// 长按手势(背景的动画效果)
- (void)longPressAction:(UILongPressGestureRecognizer *)longPress{
    // 开始长按
    if (longPress.state == UIGestureRecognizerStateBegan) {
        [UIView animateWithDuration:0.5 animations:^{
            _view.alpha = 0;
            _titleL.alpha = 0;
            _subTitleL.alpha = 0;
            _numberL.alpha = 0;
        }];
    }
    // 长按中
    if (longPress.state == UIGestureRecognizerStateChanged) {
            _view.alpha = 0;
            _titleL.alpha = 0;
            _subTitleL.alpha = 0;
            _numberL.alpha = 0;
    }
    // 长按结束
    if (longPress.state == UIGestureRecognizerStateEnded) {
        [UIView animateWithDuration:0.5 animations:^{
            _view.alpha = 0.4;
            _titleL.alpha = 1;
            _subTitleL.alpha = 1;
            _numberL.alpha = 1;
        }];
    }
}

- (void)setHotModel:(TTSHotRankModel *)hotModel{
    if (hotModel) {
        // 标题
        _titleL.text = hotModel.title;
        // 背景图片
        NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@", hotModel.coverForDetail]];
        [_bgImgView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"placeHolderBig"]];
        
        // 时间长度
        NSInteger timeIndex = [hotModel.duration intValue];
        NSString *time = [self TTSTimeformatFromSeconds:timeIndex];
        _subTitleL.text = [NSString stringWithFormat:@"#%@  /  %@", hotModel.category, time];
    }
}
// 序号
- (void)setCount:(NSInteger)count{

    _numberL.text = [NSString stringWithFormat:@"%ld.", count + 1];

}

// 时间处理
-(NSString *)TTSTimeformatFromSeconds:(NSInteger)seconds{
    //format of hour
    NSString *str_hour = [NSString stringWithFormat:@"%02ld",seconds/3600];
    //format of minute
    NSString *str_minute = [NSString stringWithFormat:@"%02ld",seconds%3600/60];
    //format of second
    NSString *str_second = [NSString stringWithFormat:@"%02ld",seconds%60];
    //format of time
    NSString *format_timeSmall = [NSString stringWithFormat:@"%@'%@''",str_minute,str_second];
    NSString *format_time = [NSString stringWithFormat:@"%@:%@:%@",str_hour,str_minute,str_second];
    if (!(seconds/3600)>0) {
        return format_timeSmall;
    }else{
        return format_time;}
}




@end
